import { getCategoryCheckList, getProductInfoForBarcode, addProduct } from '../../../utils/networkRequest.js'
import { errorMsg } from '../../../utils/tool.js'
//获取应用实例
const app = getApp()
// pages/addproduct/addproduct.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",//商品名称
    count: "",//商品数量
    in_price: "",//进价
    out_price: "",//售价
    wranCount: "",//库存预警值
    memo: "",//商品备注
    barcode: "",//商品条形码
    pic: "",//商品图片


    multiArray: [],
    multiIndex: ["", ""],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this;
    getCategoryCheckList("", (r) => {
      if (r.code == 0) {
        if (options.classindex) {  //已经进入到指定分类的商品列表，进来直接默认选中当前分类
          let classindex = JSON.parse(options.classindex)
          let id = r.data[0][classindex[0]].id;
          getCategoryCheckList(id, (rr) => {
            if (rr.code == 0) {
              r.data[1] = rr.data[1];
              self.setData({
                multiArray: r.data,
                multiIndex: classindex
              });
            } else {
              errorMsg(r.message)
            }
          })
        }else{
          self.setData({
            multiArray: r.data, 
          });
        }
      } else {
        errorMsg(r.message)
      }
    })
  },
  bindMultiPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex: e.detail.value
    })
  },
  bindMultiPickerColumnChange: function (e) {
    let self = this;
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;

    if (e.detail.column == 0) {
      let id = self.data.multiArray[0][e.detail.value].id;
      getCategoryCheckList(id, (r) => {
        if (r.code == 0) {
          data.multiArray[1] = r.data[1];
          data.multiIndex[1] = 0;
          self.setData(data);
        } else {
          errorMsg(r.message)
        }

      })

    }
  },
  //获取商品名称
  nameChange: function (e) {
    this.setData({
      name: e.detail.detail.value
    })
  },
  //获取商品数量
  countChange: function (e) {
    this.setData({
      count: e.detail.detail.value
    })
  },
  //获取商品进货价
  inPriceChange: function (e) {
    this.setData({
      in_price: e.detail.detail.value
    })
  },
  //获取商品售价
  outPriceChange: function (e) {
    this.setData({
      out_price: e.detail.detail.value
    })
  },

  //获取商品库存预警值
  wranCountChange: function (e) {
    this.setData({
      wranCount: e.detail.detail.value
    })
  },

  //获取商品备注
  memoChange: function (e) {
    this.setData({
      memo: e.detail.detail.value
    })
  },

  //扫一扫识别条形码
  onScanIcon: function () {
    let self = this;
    wx.scanCode({
      success: (res) => {
        getProductInfoForBarcode(res.result, (r) => {
          if (r.code == 0) {
            if (r.data.stock) {
              self.setData({
                name: r.data.stock.name,
                pic: r.data.stock.pic,
                count: r.data.stock.count,
                wranCount: r.data.stock.threshold,
                out_price: r.data.stock.price,
                multiIndex: r.data.stock.service_item_id,
                in_price: r.data.stock.in_price,
              })
            } else {
              self.setData({
                name: r.data.name,
                pic: r.data.pic,
                count: "",
                wranCount: "",
                out_price: "",
                multiIndex: ["", ""],
                in_price: ""
              })
            }

          }
        })
      }
    })
  },
  //添加商品
  onAddSubmit: function () {
    let { barcode, name, count, wranCount, in_price, out_price, memo, multiArray, multiIndex, pic } = this.data; 
    if (name == "") {
      errorMsg("请输入商品名称。")

    } else if (multiIndex[1] === "") {
      errorMsg("请选择分类")
    } else if (count == "") {
      errorMsg("请输入数量")
    } else {
      let itemId = multiArray[1][multiIndex[1]].id;
      addProduct(barcode, name, itemId, count, wranCount, in_price, out_price, memo, pic, (r) => {
        if (r.code == 0) {
          wx.showToast({
            title: '添加成功',
            icon: 'success',
            image: '',
            duration: 1500,
            mask: true,
            success: function (res) {
              app.globalData.addProductInfo = r.data
              wx.navigateBack();
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        }
      })
    }

  }
})